package com.pro.controller;

import com.pro.pojo.JsUserCoupon;
import com.pro.pojo.JsUserCouponUsed;
import com.pro.pojo.JsonData;
import com.pro.service.CouponService;
import com.pro.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cyj
 * @date 2022.3.17$ 13.59$
 * @content :
 */
@RestController
@RequestMapping("/newCoupon")
public class CouponController {

    @Autowired
    private CouponService service;

    @Autowired
    private FastDFSClientUtil dfsClient;

    /**
     * @author cyj
     * @date 2022.3.17$ 14.13$    15.42 高级检索
     * @content :获取所有的优惠卷
     */
    @RequestMapping("/getAllCoupon")
    public JsonData getAllCoupon(String couponName, String online, String grant, String startDate, String endDate, Integer limit, Integer page) {
        List<JsUserCoupon> allCoupon = service.getAllCoupon(couponName,online,grant,startDate,endDate,limit, page);
        Integer count = service.countCoupon();
        return allCoupon != null ? JsonData.buildSuccess(allCoupon,count): JsonData.buildError("暂无优惠卷");
    }
    /**
     * @author cyj
     * @date 2022.3.17$ 14.45$
     * @content :获取所有的优惠卷
     */
    @RequestMapping("/getCouponById")
    public JsonData getCouponById(Integer id) {
        JsUserCoupon coupon = service.getCouponById(id);
        return coupon != null ? JsonData.buildSuccess(coupon): JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.17$ 15.02$
     * @content :删除一个优惠卷
     */
    @RequestMapping("/delCoupon")
    public JsonData delCoupon(Integer id) {
        Boolean coupon = service.delCoupon(id);
        return coupon != null ? JsonData.buildSuccess(coupon): JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.17$ 15.26$
     * @content :更改一个优惠卷上下限状态
     */
    @RequestMapping("/updateOnline")
    public JsonData updateOnline(Integer couponId, Integer online, Integer updateId, String updateDate) {
        Boolean coupon = service.updateOnline(couponId,online,updateId,updateDate);
        return coupon != null ? JsonData.buildSuccess(coupon): JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.17$ 16.20$
     * @content :上传图片
     */
    @RequestMapping("/uploadFile")
    public JsonData fdfsUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        System.err.println("开始上传");
        String resAccessUrl =null;
        try {
            String fileUrl = dfsClient.uploadFile(file);
            //路径
            resAccessUrl = dfsClient.getResAccessUrl(fileUrl);

        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("path",resAccessUrl);
        return map!=null?JsonData.buildSuccess(map):JsonData.buildError("服务端错误");
    }
    /**
     * @author cyj
     * @date 2022.3.17$ 16.54$
     * @content :添加一个优惠卷
     */
    @RequestMapping("/addCoupon")
    public JsonData addCoupon(JsUserCoupon coupon) {
        Boolean flag = service.addCoupon(coupon);
        return flag != false ? JsonData.buildSuccess(flag): JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.18$ 8.34$
     * @content :获取所有的用户优惠卷
     */
    @RequestMapping("/getUserCoupons")
    public JsonData getUserCoupons(String account,String nickname,Integer limit, Integer page) {
        List<JsUserCoupon> userCoupons = service.getUserCoupons(account,nickname,limit, page);
        Integer count = service.userCouponCount(account,nickname);
        return userCoupons != null ? JsonData.buildSuccess(userCoupons,count): JsonData.buildError("用户暂无优惠卷");
    }
    /**
     * @author cyj
     * @date 2022.3.18$ 8.54$
     * @content :添加一个优惠卷
     */
    @RequestMapping("/giveCoupon")
    public JsonData giveCoupon(JsUserCouponUsed jsUserCoupon) {
        List<JsUserCouponUsed> list = new ArrayList<>();
        list.add(jsUserCoupon);
        Boolean flag = service.giveCoupon(list);
        return flag != false ? JsonData.buildSuccess(flag): JsonData.buildError("赠送失败");
    }

}
